<template>
	<view>
		<view class="flex flex-wrap" v-if="module == 'live'">
			<course-list v-for="(item,index) in list" :key="index" :item="item" :type="module == 'course' || module == 'column' ? 'one' : 'two'" :tag="tag">
				<text slot="desc" class="font-sm my-1 " :class="item.status == '直播中' ? 'text-danger' : 'text-light-muted'">{{ item.status }}</text>
			</course-list>
		</view>
		<view class="flex flex-wrap" v-else>
			<course-list v-for="(item,index) in list" :key="index" :item="item" :type="module == 'course' || module == 'column' ? 'one' : 'two'" :tag="tag"></course-list>
		</view>

		<!-- 加载loading -->
		<uni-load-more :status="loadStatus"></uni-load-more>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loadStatus: 'loading',
				page: 1,
				limit: 10,
				list: [],
				module: 'course'
			}
		},
		computed: {
			tag() {
				let t = {
					flashsale: '秒杀价',
					group: '拼团价'
				}

				return t[this.module]
			}
		},
		mounted() {
			this.getData()
		},
		onLoad(e) {
			console.log(e, 'e');
			if (e.module) {
				this.module = e.module
			}
			let t = {
				course: '课程',
				column: '专栏',
				flashsale: '秒杀',
				group: '拼团',
				live: '直播'
			}
			uni.setNavigationBarTitle({
				title: t[this.module] + '列表'
			})
		},
		// 下拉加载
		onReachBottom() {
			this.reachBottom()
		},
		// 上拉刷新
		onPullDownRefresh() {
			this.getData()
		},
		methods: {
			getData() {
				let fun = {
					course: 'getCourseList',
					column: 'getColumnList',
					group: 'getGroupList',
					flashsale: 'getFlashsaleList',
					live: 'getLiveList'
				}
				this.loadStatus = 'loading'
				this.$api[fun[this.module]]({
					page: this.page,
					limit: this.limit
				}).then(res => {
					if (this.module === 'live') {
						res.rows = res.rows.map(item => {
							item.type ='live'
							console.log(item,'item');
							let startTime = new Date(item.start_time).getTime()
							let endTime = new Date(item.end_time).getTime()
							let now = Date.now()
							console.log(startTime, endTime, now);
							if (startTime > now) {
								item.status = '未开始'
							} else if (now > endTime) {
								item.status = '已结束'
							} else {
								item.status = '直播中'
							}
							item.type = 'live'
							return item
						})
					}
					this.list = this.page === 1 ? res.rows : [...this.list, ...res.rows]
					
					if (res.rows.length < this.limit) {
						this.loadStatus = 'noMore'
					} else if (res.rows.length === this.limit) {
						this.loadStatus = 'more'
					}
				}).catch(err => {
					this.loadStatus = 'more'
					if (this.page > 1) {
						this.page -= 1
					}
				}).finally(() => {
					uni.stopPullDownRefresh()
				})
			},
			reachBottom() {
				if (this.loadStatus !== 'more') return
				this.page++
				this.getData()
			},
		}
	}
</script>

<style>

</style>
